// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

        window._TESTMAP = this;
        this.titledMap = this.node.getChildByName('map').getComponent(cc.TiledMap)
        this.mapObj = {};
        this.mapConfigObj = {};
        this.alltargetMap = {};
        this.armyAreaLiveMap = {};
        this.bossMap={};
        this.mapArr = [];
        this.mapIndex = 0;

        for(var key in _MAINTASK){
            var mapname = _MAINTASK[key].map;
            if(_MAPCONFIG[mapname]){
                this.bossMap[mapname]=2;
            }else{
                console.log(key,mapname+"不存在");
            }
        }

        for(var key in window._NPCMAP){
            this.armyAreaLiveMap[key]=0;
        }

        for(var key in _MAPCONFIG){

            var mapConfig = _MAPCONFIG[key];
            var path = mapConfig.path;
            if(this.mapObj[path]){
                //console.log('重复配置:'+key+' '+path);
            }else{
                this.mapObj[path]={name:key};
                this.mapArr.push(path);
            }

        }

        console.log("#######扫描开始");
        this.loadAndTestMap();
    },

    loadAndTestMap:function(){

        var mappath = this.mapArr[this.mapIndex];
        var testMapPath = "map/"+mappath;
        
        //console.log("扫描："+this.mapIndex+" "+testMapPath)

        cc.loader.loadRes(testMapPath, function(err, map){

            _TESTMAP.titledMap.tmxAsset = map;

            var jumpMap = _TESTMAP.initJump();

            var targetMap = _TESTMAP.initTarget();

            var tempMapObj = _TESTMAP.mapObj[_TESTMAP.mapArr[_TESTMAP.mapIndex]];

            tempMapObj.jumpMap = jumpMap;
            tempMapObj.targetMap = targetMap;

            for(var key in _TESTMAP.armyAreaLiveMap){

                var layerName = "野区_"+key;
                if(_TESTMAP.titledMap.getLayer(layerName)){
                    _TESTMAP.armyAreaLiveMap[key]=1;
                    if(key!=mappath){
                        console.error("野区异常 地图:"+mappath+" 图层:"+layerName);
                    }
                }

            }

            if(
                _TESTMAP.titledMap.getObjectGroup("boss")
                &&_TESTMAP.titledMap.getObjectGroup("boss").getObjects()
                &&_TESTMAP.titledMap.getObjectGroup("boss").getObjects().length==1
            ){
                var mapname = _TESTMAP.mapObj[mappath].name;
                if(_TESTMAP.bossMap[mapname])
                    _TESTMAP.bossMap[mapname]=1;
            }

            _TESTMAP.mapIndex++;

            if(_TESTMAP.mapIndex>=_TESTMAP.mapArr.length){

                console.log("#######扫描结束");
                //console.log(_TESTMAP.mapObj);
                _TESTMAP.checkMapConfig();
            }else{

                _TESTMAP.loadAndTestMap();

            }

        });

    },

    checkMapConfig:function(){
        console.log("#######开始校验配置");
        for(var key in _MAPCONFIG){
            if(!this.alltargetMap[key]){
                console.error("【未发现传送】 配置名:"+key);
            }
            var mapConfig = _MAPCONFIG[key];
            var mapscanConfig = _TESTMAP.mapObj[mapConfig.path];
            if(mapscanConfig.jumpMap){//校验地图文件所有跳转

                for(var jumptarget in mapscanConfig.jumpMap){
                    var mapname = mapscanConfig.jumpMap[jumptarget].map;
                    var maptarget = mapscanConfig.jumpMap[jumptarget].target;

                    if(_MAPCONFIG[mapname]){
                        var mapscanConfig2 = _TESTMAP.mapObj[_MAPCONFIG[mapname].path];
                        if(mapscanConfig2){
                            if(!mapscanConfig2.targetMap||!mapscanConfig2.targetMap[maptarget]){
                                console.error("【跳转target不存在】 "+" 地图文件:"+mapConfig.path+" 跳转配置:"+mapname+"_"+maptarget+" 目标地图文件:"+_MAPCONFIG[mapname].path+" 缺少target:"+maptarget);
                            }
                        }else{
                            console.error("【目标地图文件不存在】 "+"地图文件:"+_MAPCONFIG[mapname].path);
                        }
                    }else{
                        console.error("【配置不存在】 "+"地图文件:"+mapConfig.path+" 跳转配置:"+mapname+"_"+maptarget+" 缺少地图配置:"+mapname);
                    }
                }

            }
        }
        for(var key in _TESTMAP.armyAreaLiveMap){
            if(_TESTMAP.armyAreaLiveMap[key]==0){
                console.error("【野区不存在】:"+key);
            }
        }
        console.log("#######校验配置结束");
        var bossmapnmb=0;
        for(var key in _TESTMAP.bossMap){
            if(_TESTMAP.bossMap[key]==2){
                console.log(key+" 缺失boss图层");
                bossmapnmb++;
            }
        }
        console.log('总共缺失boss图层:'+bossmapnmb);
    },

    initJump:function(){
        if(this.titledMap.getObjectGroup("jump")){
            var jumpPoints = this.titledMap.getObjectGroup("jump").getObjects();
            var jumpMap = null;
            for(var i=0;i<jumpPoints.length;i++){

                if(jumpPoints[i].name){
                    var arr = jumpPoints[i].name.split("_");
                    var mappos = _MANAGERMAP.getMapObjPos(jumpPoints[i]);

                    if(arr.length>=2){

                        if(jumpMap==null){
                            jumpMap={};
                        }

                        jumpMap[mappos.x+"-"+mappos.y]={map:arr[0],target:arr[1]};
                        this.alltargetMap[arr[0]]=1;
                    }
                }

            }
            
            return jumpMap;

        }

        return null;
    },

    initTarget:function(){

        if(this.titledMap.getObjectGroup("target")){
            var jumpPoints = this.titledMap.getObjectGroup("target").getObjects();
            var jumpMap = {};
            for(var i=0;i<jumpPoints.length;i++){

                if(jumpPoints[i].name){
                    var mappos = _MANAGERMAP.getMapObjPos(jumpPoints[i]);
                    jumpMap[jumpPoints[i].name]=mappos.x+"-"+mappos.y;
                }

            }
            
            return jumpMap;

        }

        return null;

    }

    // update (dt) {},
});
